// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Debug Mode Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 3 : TYPE_DEBUGMODE

// Function declarations
reserve function DebugMode_SpawnObject
reserve function DebugMode_Blank
reserve function DebugMode_DrawLifeIcon
reserve function DebugMode_DeleteObject
reserve function DebugMode_DeleteDraw
reserve function DebugMode_LoadCustom
reserve function DebugMode_LoadDraw
reserve function DebugMode_SaveCustom
reserve function DebugMode_SaveDraw
reserve function DebugMode_ClearLevel
reserve function DebugMode_ClearDraw
reserve function DebugMode_TargetonScreen

// Static Values
public value DebugMode_ObjCount = 0;
public value Slot.xpos = 0;
public value Slot.ypos = 0;

// Tables
public table DebugMode_TypeTable
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table

public table DebugMode_DrawTable
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table

public table DebugMode_SpawnTable
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
end table


function DebugMode_SpawnObject
	temp0 = 0
	temp1 = 0
	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Blank Object] //Find unused object slots
			if temp1 == 0 
				temp1 = arrayPos0
			end if
		end if
		if Slot.xpos == object[arrayPos0].xpos //Find objects in the targeted slot
			if Slot.ypos == object[arrayPos0].ypos
				if temp0 == 0
					temp0 = arrayPos0 //Mark the first one
				end if
				//Any objects already in that position? Kill em
				//this fixes a bug that made objects undeletable
				ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0) 
			end if
		end if
		arrayPos0++
	loop
	if temp0 != 0 //Use the object slot already here
		arrayPos0 = temp0
		ResetObjectEntity(temp0, temp5, temp4, Slot.xpos, Slot.ypos)
	else 
		if temp1 != 0//Use the first blank object slot we found
			ResetObjectEntity(temp1, temp5, temp4, Slot.xpos, Slot.ypos)
			arrayPos0 = temp1
		else
			//Object slots full, and yet no objects at the target position to overwrite
			//Unlikely but not impossible
			//Honestly don't know what to do here. For now, no new object spawns
		end if
	end if
end function


function DebugMode_Blank
	temp0 = 0
end function


function DebugMode_DrawLifeIcon
	DrawSprite(0)
end function


function DebugMode_DeleteObject
	temp5 = 0
	temp4 = 0
	CallFunction(DebugMode_SpawnObject)
end function


function DebugMode_DeleteDraw
	DrawSprite(2)
end function


function DebugMode_LoadCustom //Load Custom Special
	arrayPos0 = 32
	arrayPos1 = 0x800
	while arrayPos0 < 0x420
		temp0 = saveRAM[arrayPos1]
		temp0 &= 255
		temp1 = saveRAM[arrayPos1]
		temp1 >>= 8
		arrayPos1++
		if temp0 != TypeName[Blank Object]
			temp2 = saveRAM[arrayPos1]
			temp2 &= 0x7FF
			temp2 *= 0x180000
			temp2 += 0xC0000
			temp3 = saveRAM[arrayPos1]
			temp3 >>= 11
			temp3 *= 0x180000
			temp3 += 0xC0000
			ResetObjectEntity(arrayPos0, temp0, temp1, temp2, temp3)
		end if
		arrayPos1++
		arrayPos0++
	loop
	object.xpos = temp2
	object.ypos = temp3
end function


function DebugMode_LoadDraw
	DrawSprite(3)
end function


function DebugMode_SaveCustom //Save Custom Special
	arrayPos0 = 32
	arrayPos1 = 0x800
	while arrayPos0 < 0x420
		if object[arrayPos0].type != TypeName[Blank Object] //no need to save blank objects
			temp2 = object[arrayPos0].propertyValue
			temp2 <<= 8
			temp2 += object[arrayPos0].type		
			saveRAM[arrayPos1] = temp2
			arrayPos1++
			temp0 = object[arrayPos0].xpos
			temp0 /= 0x180000
			temp1 = object[arrayPos0].ypos
			temp1 /= 0x180000
			if temp0 < 0
				temp0 -= 0x180000
			end if
			if temp1 < 0
				temp1 -= 0x180000
			end if
			temp1 <<= 11
			temp0 += temp1
			saveRAM[arrayPos1] = temp0
			arrayPos1++
		end if
		arrayPos0++
	loop
	while arraypos1 < 0x1000 //make sure to wipe the unused space
		saveRAM[arrayPos1] = 0
		arrayPos1++
	loop
	WriteSaveRAM()
end function


function DebugMode_SaveDraw
	DrawSprite(4)
end function


function DebugMode_ClearLevel
	temp0 = 0
	arrayPos0 = 32
	while arrayPos0 < 0x420
		ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		arrayPos0++
	loop
end function


function DebugMode_ClearDraw
	DrawSprite(5)
end function


function DebugMode_TargetonScreen
	temp2 = Slot.xpos
	temp2 -= object[0].xpos
	temp2 >>= 8
	temp3 = Slot.ypos
	temp3 -= object[0].ypos
	temp3 >>= 8
	Sin(temp4, PlayerObject_value1)
	temp4 *= temp3
	Cos(temp5, PlayerObject_value1)
	temp5 *= temp2
	temp0 = temp4
	temp0 += temp5
	temp0 >>= 1
	temp0 += object[0].xpos
	Cos(temp4, PlayerObject_value1)
	temp4 *= temp3
	Sin(temp5, PlayerObject_value1)
	temp5 *= temp2
	temp1 = temp4
	temp1 -= temp5
	temp1 >>= 1
	temp1 += object[0].ypos
	object.rotation = 512
	object.rotation -= PlayerObject_value1
end function


event ObjectMain
	if saved.mobile == 1
		if object.controlMode == 0
			CheckTouchRect(0, 96, screen.xcenter, screen.ysize)
			if checkResult > -1
				arrayPos0 = checkResult
				temp0 = touchscreen[arrayPos0].xpos
				temp0 -= 56
				temp1 = touchscreen[arrayPos0].ypos
				temp1 -= 184
				ATan2(temp2, temp0, temp1)
				temp2 += 16
				temp2 &= 255
				temp2 >>= 5
				switch temp2
				case 0
					inputDown.right = true
					break
				case 1
					inputDown.right = true
					inputDown.down = true
					break
				case 2
					inputDown.down = true
					break
				case 3
					inputDown.down = true
					inputDown.left = true
					break
				case 4
					inputDown.left = true
					break
				case 5
					inputDown.left = true
					inputDown.up = true
					break
				case 6
					inputDown.up = true
					break
				case 7
					inputDown.up = true
					inputDown.right = true
					break
				end switch
			end if

			temp0 = screen.xcenter
			temp0 -= 48
			temp1 = temp0
			temp1 += 96

			CheckTouchRect(0, 0, 82, 56)
			if checkResult > -1
				arrayPos0 = checkResult
				if touchscreen[arrayPos0].xpos > screen.xcenter
					if touchJump == 0
						object.value10++
						if object.value10 >= DebugMode_ObjCount
							object.value10 = 0
						end if
					end if
					touchJump = true
				else
					if touchJump == 0
						object.value10--
						if object.value10 < 0
							object.value10 += DebugMode_ObjCount
						end if
					end if
					touchJump = true
				end if
			else
				CheckTouchRect(screen.xcenter, 96, screen.xsize, 240)
				switch checkResult
				case -1
					touchJump = 0
					break
				case 0
					inputDown.buttonC = true
					if touchJump == 0
						inputPress.buttonC |= inputDown.buttonC
					end if
					touchJump = inputDown.buttonC
					break
				end switch
			end if

			CheckTouchRect(temp0, 0, temp1, 56)
			if checkResult > -1
				inputDown.buttonB = true
			end if
			if touchDebug == false
				inputPress.buttonB |= inputDown.buttonB
			end if
			touchDebug = inputDown.buttonB
			temp0 = screen.xsize
			temp0 -= 75

			CheckTouchRect(temp0, 0, screen.xsize, 40)
			if checkResult > -1
				CreateTempObject(TypeName[Fade In], 10, object.xpos, object.ypos)
				stage.state = STAGE_PAUSED
			end if
		end if
	end if

	if object.controlMode == 0
		if inputPress.start == 1
			if stage.pauseEnabled == 1
				CreateTempObject(TypeName[Fade In], 10, object.xpos, object.ypos)
			end if
		end if
	end if
	temp0 = 0
	if inputDown.left == 1
		object.xpos -= object.yvel
		temp0 = 1
	end if
	if inputDown.right == 1
		object.xpos += object.yvel
		temp0 = 1
	end if
	if inputDown.up == 1
		object.ypos -= object.yvel
		temp0 = 1
	end if
	if inputDown.down == 1
		object.ypos += object.yvel
		temp0 = 1
	end if
	if temp0 == 1
		object.yvel += 0xC00
		if object.yvel > 0x100000
			object.yvel = 0x100000
		end if
	else
		object.yvel = 0
	end if
	if inputPress.buttonB == 1
		object.type = TypeName[Player Object]
		if stage.playerListPos == 1
			object[+1].type = TypeName[Tails Object]
		end if
		object.speed = 0
		object.xvel = 0
		object.yvel = 0
		object.frame = 0
		object.controlMode = 0
		object.interaction = 1
		PlayerObject_value2 = 0
		PlayerObject_value3 = 0
		object.value12 = 0
		object.value13 = 0
	else
		if inputPress.buttonA == 1
			object.value10++
			if object.value10 >= DebugMode_ObjCount
				object.value10 = 0
			end if
		end if
		if inputDown.buttonA == 1
			if inputPress.buttonC == 1
				object.value10--
				if object.value10 < 0
					object.value10 += DebugMode_ObjCount
				end if
			end if
		else
			if inputPress.buttonC == 1
				GetTableValue(temp0, object.value10, DebugMode_SpawnTable)
				CallFunction(temp0)
				object[tempObjectPos].priority = PRIORITY_ACTIVE
			end if
		end if
	end if
end event


event ObjectDraw
	Slot.xpos = object.xpos
	Slot.ypos = object.ypos
	if Slot.xpos < 0
		Slot.xpos -= 0x180000
	end if
	if Slot.ypos < 0
		Slot.ypos -= 0x180000
	end if
	Slot.xpos /= 0x180000
	Slot.ypos /= 0x180000
	Slot.xpos *= 0x180000
	Slot.ypos *= 0x180000
	Slot.xpos += 0xC0000
	Slot.ypos += 0xC0000
	CallFunction(DebugMode_TargetonScreen)
	DrawSpriteXY(1, temp0, temp1)
	GetTableValue(object.type, object.value10, DebugMode_TypeTable)
	GetTableValue(temp0, object.value10, DebugMode_DrawTable)
	CallFunction(temp0)
	object.type = TypeName[Debug Mode]

	// +/-
	if saved.mobile == 1
		DrawSpriteScreenXY(6, 35, 16)
		DrawSpriteScreenXY(7, 45, 16)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-12, -12, 24, 24, 251, 176) //0 Life Icon
	SpriteFrame(-12, -12, 24, 24, 131, 454) //1 Target for new object
	SpriteFrame(-12, -12, 24, 24, 131, 479) //2 Delete Icon
	SpriteFrame(-12, -12, 24, 24, 455, 486) //3 Load Icon
	SpriteFrame(-12, -12, 24, 24, 480, 486) //4 Save Icon
	SpriteFrame(-12, -12, 24, 24, 308, 376) //5 Clear Icon
	SpriteFrame(-16, 0, 8, 8, 151, 392)
	SpriteFrame(8, 0, 8, 8, 160, 392)
	DebugMode_ObjCount = 0
	temp0 = 0
	//while temp0 < 64 //What is this for??
	//	SetTableValue(TypeName[Debug Mode], temp0, DebugMode_TypeTable)
	//	SetTableValue(DebugMode_DrawLifeIcon, temp0, DebugMode_DrawTable)
	//	SetTableValue(DebugMode_Blank, temp0, DebugMode_SpawnTable)
	//	temp0++
	//loop
	SetTableValue(TypeName[Debug Mode], temp0, DebugMode_TypeTable)
	SetTableValue(DebugMode_DeleteDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(DebugMode_DeleteObject, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
	if stage.actNum == 8 //Custom level only
		SetTableValue(TypeName[Debug Mode], DebugMode_ObjCount, DebugMode_TypeTable)
		SetTableValue(DebugMode_LoadDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(DebugMode_LoadCustom, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		SetTableValue(TypeName[Debug Mode], DebugMode_ObjCount, DebugMode_TypeTable)
		SetTableValue(DebugMode_SaveDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(DebugMode_SaveCustom, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		SetTableValue(TypeName[Debug Mode], DebugMode_ObjCount, DebugMode_TypeTable)
		SetTableValue(DebugMode_ClearDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(DebugMode_ClearLevel, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
